<template>
  <div class="" v-loading.lock="loading" element-loading-text="拼命加载中">
    <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="80px">
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="企业名称" prop="title">
            <el-input v-model="dataForm.title" type="text" placeholder="客户名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="审核状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="请选择审核状态">
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <el-form-item label="录入时间" prop="sale">
            <el-date-picker
              v-model="dataForm.created_at"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :xs="20" :sm="20" :md="12" :lg="12" :xl="12">
          <el-form-item>
            <el-button @click="getDataList()" type="primary">查询</el-button>
            <el-button @click="refundFormHandle()" type="danger">退款</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataList" border :row-class-name="rowClassName" v-loading="dataListLoading" @sort-change="changeSort" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column  prop="id" header-align="center" align="center" width="80" label="ID"></el-table-column>
      <el-table-column prop="type" header-align="center" width="80" align="center" label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1" size="small" type="danger">充值</el-tag>
          <el-tag v-else size="small">退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="企业名称"></el-table-column>
      <el-table-column prop="payer_number" header-align="center" align="center" label="付款账号"></el-table-column>
      <!--
      <el-table-column prop="pay_type" header-align="center" align="center" label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.pay_type === 'alipay'" size="small">支付宝</el-tag>
          <el-tag v-if="scope.row.pay_type === 'zsbank'" size="small">浙商</el-tag>
          <el-tag v-if="scope.row.pay_type === 'mayibank'" size="small">网商</el-tag>
          <el-tag v-if="scope.row.pay_type === 'wechat'" size="small">微信</el-tag>
        </template>
      </el-table-column>
      -->
      <el-table-column prop="amount" header-align="center" align="center" width="120" label="金额">
        <template slot-scope="scope">
          <span class="danger-text">￥{{scope.row.amount}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" header-align="center" align="center" width="200" label="付款时间"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" width="200" label="备注"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" width="200" label="审核状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status === -1" style="color: #F56C6C">审核拒绝</span>
          <span v-else-if="scope.row.status === 1">等待审核</span>
          <span v-else style="color: #67C23A">审核通过</span>
          <el-popover
            placement="right" width="300" trigger="hover">
            <span slot="reference" style="color: #8896b3;margin-left: 5px;" class="el-icon-info"></span>
            <div class="recharge-info">
              <div class="row">
                <div class="label">付款方姓名</div>
                <div class="value">{{scope.row.payer_name}}</div>
              </div>
              <div class="row">
                <div class="label">银行账户</div>
                <div class="value">{{scope.row.payer_number}}</div>
              </div>
              <div class="row">
                <div class="label">备注</div>
                <div class="value">{{scope.row.remark}}</div>
              </div>
              <div class="row" v-if="scope.row.reason">
                <div class="label">审核原因</div>
                <div class="value">{{scope.row.reason}}</div>
              </div>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="140"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('recharge:check') && scope.row.status === 1" size="mini" type="danger" @click="checkFormHandle(scope.row.id)">审核</el-button>
          <el-button v-else size="mini" type="text">-</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 弹窗, 审核 -->
    <check-form v-if="checkVisible" ref="checkForm" @refreshDataList="getDataList"></check-form>

    <!-- 退款 -->
    <refund-form v-if="refundVisible" ref="refundForm" @refreshDataList="getDataList"></refund-form>

  </div>
</template>

<script>
  import CheckForm from './recharge-check'
  import RefundForm from './recharge-refund'
  export default {
    data () {
      return {
        dataForm: {
          title: '',
          status: '',
          created_at: '',
          sort_by: '',
          order_by: ''
        },
        status: [{
          value: '',
          label: "全部"
        },{
          value: -1,
          label: "审核拒绝"
        },{
          value: 1,
          label: "等待审核"
        },{
          value: 2,
          label: "审核通过"
        }],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        checkVisible: false,
        refundVisible: false,
        loading: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }
      }
    },
    components: {
      CheckForm,
      RefundForm
    },
    activated () {
      this.getDataList()
    },
    methods: {
      rowClassName({row, rowIndex}) {
        if(row.is_jg) {
          return 'bgred';
        }
        return ''
      },
      changeSort(val) {
        if (val.order !== null) {
          this.dataForm.sort_by = val.prop
          this.dataForm.order_by = val.order === 'ascending' ? 'asc' : 'desc'
        } else {
          this.dataForm.sort_by = ''
          this.dataForm.order_by = ''
        }
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.getCustomerList()
        this.dataListLoading = false
      },
      getCustomerList() {
        this.$http({
          url: this.$http.adornUrl('/recharge/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'title': this.dataForm.title,
            'status': this.dataForm.status,
            'created_at': this.dataForm.created_at,
            'sort': this.dataForm.sort_by,
            'order': this.dataForm.order_by
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      checkFormHandle (id) {
        this.checkVisible = true
        this.$nextTick(() => {
          this.$refs.checkForm.init(id)
        })
      },
      refundFormHandle() {
        this.refundVisible = true
        this.$nextTick(() => {
          this.$refs.refundForm.init()
        })
      }
    }
  }
</script>

<style lang="scss">
  .bgred {
    background-color: #fab6b6 !important;
  }

  .recharge-info {
    .row {
      display: flex;
      line-height: 24px;
    }
    .label {
      width: 80px;
      text-align: right;
      color: #999;
    }
    .value {
      flex: 1;
      padding-left: 20px;
    }
  }
</style>
